package com.demo.autotest.service;

import com.demo.autotest.common.exception.DuplicateException;
import com.demo.autotest.database.domain.CommonParam;
import com.demo.autotest.database.mapper.CommonParamMapper;
import com.demo.autotest.dto.CommonParamDTO;
import com.demo.autotest.request.QueryRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service    // 定义service层
@Transactional(rollbackFor = Exception.class)
public class CommonParamService {

    @Resource
    private CommonParamMapper commonParamMapper;

    public void saveParam(CommonParam commonParam){
        CommonParam old = commonParamMapper.getParamByName(commonParam.getProjectId(), commonParam.getName());
        if(old != null && !old.getId().equals(commonParam.getId())){
            throw new DuplicateException("本项目已有该参数名称");
        }
        if(commonParam.getId() != null && !commonParam.getId().equals("")){
            commonParam.setUpdateTime(System.currentTimeMillis());
        }else{
            commonParam.setId(UUID.randomUUID().toString());
            commonParam.setCreateUser(commonParam.getUpdateUser());
            commonParam.setCreateTime(System.currentTimeMillis());
            commonParam.setUpdateTime(System.currentTimeMillis());
        }
        commonParamMapper.saveParam(commonParam);
    }

    public void delete(String id){
        commonParamMapper.delete(id);
    }

    public List<CommonParamDTO> getCommonParamList(QueryRequest request){
        if(request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition("%" + request.getCondition() + "%");
        }
        return commonParamMapper.getCommonParamList(request);
    }

}
